using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.TTC.Accessors;

namespace OracleInternal.TTC
{
	internal class TTCColumnMetaData
	{
		internal const short UACFIND = 1;

		internal const short UACFALN = 2;

		internal const short UACFRCP = 4;

		internal const short UACFBBV = 8;

		internal const short UACFNCP = 16;

		internal const short UACFBLP = 32;

		internal const short UACFARR = 64;

		internal const short UACFIGN = 128;

		internal const int UACFNSCL = 1;

		internal const int UACFBUC = 2;

		internal const int UACFSKP = 4;

		internal const int UACFCHRCNT = 8;

		internal const int UACFNOADJ = 16;

		internal const int UACFCUS = 4096;

		internal const int UACFLSZ = 33554432;

		internal static void ReadMessage(MarshallingEngine mEngine, ColumnDescribeInfo colMetaData, bool bIgnoreMetadata)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCColumnMetaData, OracleTraceFuncName.ReadMessage);
			}
			try
			{
				if (!bIgnoreMetadata)
				{
					colMetaData.m_dataType = mEngine.UnmarshalUB1();
					colMetaData.m_flag = mEngine.UnmarshalUB1();
					colMetaData.m_precision = mEngine.UnmarshalUB1();
					if (colMetaData.m_dataType == 2 || colMetaData.m_dataType == 187 || colMetaData.m_dataType == 180 || colMetaData.m_dataType == 188 || colMetaData.m_dataType == 181 || colMetaData.m_dataType == 232 || colMetaData.m_dataType == 231 || colMetaData.m_dataType == 190 || colMetaData.m_dataType == 183)
					{
						colMetaData.m_scale = (short)mEngine.UnmarshalUB2();
					}
					else
					{
						colMetaData.m_scale = mEngine.UnmarshalUB1();
					}
					if (colMetaData.m_scale == -127)
					{
						colMetaData.m_precision = (short)Math.Ceiling((double)colMetaData.m_precision * 0.30103);
						colMetaData.m_scale = 127;
					}
					if (colMetaData.m_dataType == 2 && colMetaData.m_precision == 0 && (colMetaData.m_scale == 0 || colMetaData.m_scale == 127))
					{
						colMetaData.m_precision = 38;
						colMetaData.m_scale = 127;
					}
					colMetaData.m_maxLength = mEngine.UnmarshalSB4();
					if (colMetaData.m_maxLength != 0)
					{
						if (colMetaData.m_dataType == 101)
						{
							colMetaData.m_maxLength = TTCBinaryDoubleAccessor.BINARY_DOUBLE_MAX_LENGTH;
						}
						else if (colMetaData.m_dataType == 100)
						{
							colMetaData.m_maxLength = TTCBinaryFloatAccessor.BINARY_FLOAT_MAX_LENGTH;
						}
						else if (colMetaData.m_dataType == 12)
						{
							colMetaData.m_maxLength = 7;
						}
						else if (colMetaData.m_dataType == 190 || colMetaData.m_dataType == 183 || colMetaData.m_dataType == 189 || colMetaData.m_dataType == 182)
						{
							colMetaData.m_maxLength = TTCIntervalTypeAccessor.INTERVALTYPE_MAX_LENGTH;
						}
						else if (colMetaData.m_dataType == 11)
						{
							colMetaData.m_maxLength = 128;
						}
						else if (colMetaData.m_dataType == 181)
						{
							colMetaData.m_maxLength = 13;
						}
					}
					colMetaData.m_maxNoOfArrayElements = mEngine.UnmarshalSB4();
					if (mEngine.NegotiatedTTCVersion >= 10)
					{
						colMetaData.m_contFlag = mEngine.UnmarshalSB8();
					}
					else
					{
						colMetaData.m_contFlag = mEngine.UnmarshalSB4();
					}
					byte[] array = mEngine.UnmarshalDALC(bIgnoreData: false, mEngine.retLen);
					if (array != null)
					{
						colMetaData.m_toid = new byte[mEngine.retLen[0]];
						Buffer.BlockCopy(array, 0, colMetaData.m_toid, 0, mEngine.retLen[0]);
					}
					colMetaData.m_version = mEngine.UnmarshalUB2();
					colMetaData.m_characterSetId = mEngine.UnmarshalUB2();
					colMetaData.m_characterSetForm = mEngine.UnmarshalUB1();
					colMetaData.m_maxLengthOfChars = (int)mEngine.UnmarshalUB4();
					if (mEngine.NegotiatedTTCVersion >= 8)
					{
						colMetaData.m_oaccollid = (int)mEngine.UnmarshalUB4();
					}
				}
				else
				{
					mEngine.UnmarshalUB1(bIgnoreData: true);
					mEngine.UnmarshalUB1(bIgnoreData: true);
					mEngine.UnmarshalUB1(bIgnoreData: true);
					if (colMetaData.m_dataType == 2 || colMetaData.m_dataType == 187 || colMetaData.m_dataType == 180 || colMetaData.m_dataType == 188 || colMetaData.m_dataType == 181 || colMetaData.m_dataType == 232 || colMetaData.m_dataType == 231 || colMetaData.m_dataType == 190 || colMetaData.m_dataType == 183)
					{
						mEngine.UnmarshalUB2(bIgnoreData: true);
					}
					else
					{
						mEngine.UnmarshalUB1(bIgnoreData: true);
					}
					mEngine.UnmarshalUB4(bIgnoreData: true);
					mEngine.UnmarshalUB4(bIgnoreData: true);
					if (mEngine.NegotiatedTTCVersion >= 10)
					{
						mEngine.UnmarshalSB8();
					}
					else
					{
						mEngine.UnmarshalSB4();
					}
					mEngine.UnmarshalDALC(bIgnoreData: true);
					mEngine.UnmarshalUB2(bIgnoreData: true);
					mEngine.UnmarshalUB2(bIgnoreData: true);
					mEngine.UnmarshalUB1();
					mEngine.UnmarshalUB4(bIgnoreData: true);
					if (mEngine.NegotiatedTTCVersion >= 8)
					{
						mEngine.UnmarshalUB4(bIgnoreData: true);
					}
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCColumnMetaData, OracleTraceFuncName.ReadMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCColumnMetaData, OracleTraceFuncName.ReadMessage);
				}
			}
		}

		internal static void WriteMessage(MarshallingEngine mEngine, ColumnDescribeInfo colMetaData)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCColumnMetaData, OracleTraceFuncName.WriteMessage);
			}
			try
			{
				mEngine.MarshalUB1(colMetaData.m_dataType);
				mEngine.MarshalUB1(colMetaData.m_flag);
				mEngine.MarshalUB1(colMetaData.m_precision);
				mEngine.MarshalUB1(colMetaData.m_scale);
				mEngine.MarshalUB4(colMetaData.m_maxLength);
				mEngine.MarshalSB4(colMetaData.m_maxNoOfArrayElements);
				if (mEngine.NegotiatedTTCVersion >= 10)
				{
					mEngine.MarshalSB8(colMetaData.m_contFlag);
				}
				else
				{
					mEngine.MarshalSB4((int)colMetaData.m_contFlag);
				}
				mEngine.MarshalDALC(colMetaData.m_toid);
				mEngine.MarshalUB2(colMetaData.m_version);
				mEngine.MarshalUB2(colMetaData.m_characterSetId);
				mEngine.MarshalUB1(colMetaData.m_characterSetForm);
				mEngine.MarshalUB4(colMetaData.m_maxLengthOfChars);
				if (mEngine.NegotiatedTTCVersion >= 8)
				{
					mEngine.MarshalUB4(colMetaData.m_oaccollid);
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCColumnMetaData, OracleTraceFuncName.WriteMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCColumnMetaData, OracleTraceFuncName.WriteMessage);
				}
			}
		}
	}
}
